package com.henu.art.service.Imlp;

import com.henu.art.entity.Teacher;
import com.henu.art.mapper.TeacherMapper;
import com.henu.art.service.AuthService;
import com.henu.art.util.EncodeUtil;
import com.henu.art.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zoey
 * \\_/__/
 * @Date: 2025/04/24/19:31
 * @Description:
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private CustomRedis customRedis;

    @Override
    public boolean login(String username, String password) throws Exception {
        password = EncodeUtil.encodePassword(password);
        if (username.equals(customRedis.getAdminUsername())) {
            if (password.equals(customRedis.getAdminPassword())) {
                return true;
            }
            throw new Exception("管理员密码错误");
        }
        Teacher teacher = teacherMapper.selectById(username);
        if (teacher != null) {
            return teacher.getPassword()
                    .equals(password);
        }
        return false;
    }

    @Override
    public String getUsernameByToken(String token) {
        return JWTUtil.getUserInfoByToken(token).get("username");
    }
}
